//
//  DZMReadConfigure.swift
//  BookRead
//
//  Created by fox on 2017/5/11.
//  Copyright © 2017年 DZM. All rights reserved.
//
/// 翻页类型
enum DZMRMEffectType:NSInteger {
//    case none               // 无效果
     case simulation         // 仿真
     case translation        // 平移
   
//    case upAndDown          // 上下
}

/// 字体类型
enum DZMRMFontType:NSInteger {
    case system             // 系统
    case one                // 黑体
    case two                // 楷体
    case three              // 宋体
}
/// key
let DZMReadConfigureKey:String = "ReadConfigure"

/// 单利对象
private var instance:DZMReadConfigure? = DZMReadConfigure.readInfo()

// MARK: -- 配置属性

/// 背景颜色数组
let DZMReadBGColors:[UIColor] = [UIColor.white,DZMReadBGColor_1,DZMReadBGColor_2,DZMReadBGColor_3,DZMReadBGColor_4,DZMReadBGColor_5]

/// 根据背景颜色 对应的文字颜色 数组(数量必须与 DZMReadBGColors 相同)
 let DZMReadTextColors:[UIColor] = [DZMColor_7,DZMColor_7,DZMColor_7,DZMColor_7,DZMColor_7,DZMColor_5]

/// 阅读最小阅读字体大小
let DZMReadMinFontSize:NSInteger = 14

/// 阅读最大阅读字体大小
let DZMReadMaxFontSize:NSInteger = 38

/// 阅读当前默认字体大小
let DZMReadDefaultFontSize:NSInteger = 20

import UIKit

class DZMReadConfigure: NSObject {

      static var rect: CGRect = CGRect.init(x: 12, y: 70, width: ScreenWidth - 24, height: ScreenHeight - 70)
    
    /// 当前阅读的背景颜色
    var colorIndex:NSInteger = 1 {didSet{save()}}

    var  paragraphStyleIndex:NSInteger = 0 {didSet{save()}}
    /// 字体类型
    var fontType:NSInteger = DZMRMFontType.system.rawValue {didSet{save()}}
    
    /// 字体大小
    var fontSize:NSInteger = DZMReadDefaultFontSize {didSet{save()}}
    
    /// 翻页效果
    var effectType:NSInteger = DZMRMEffectType.simulation.rawValue {didSet{save()}}
    
    
    /// 默认是垂直
    var typePage:UIPageViewControllerNavigationOrientation = .horizontal
    
    var offectypeTop:NSInteger = 0
    /// 阅读文字颜色(更加需求自己选)
    var textColor:UIColor {
        
        // 固定颜色使用
        get
        {
            return DZMColor_5
        }
    }
    
    // MARK: -- 操作
    
    /// 单例
    class func shared() ->DZMReadConfigure {
        
        if instance == nil {
            
            instance = DZMReadConfigure.readInfo()
        }
        
        return instance!
    }
    
    /// 保存
    func save() {
        
        var dict = allPropertys()
        
        dict.removeValue(forKey: "lineSpacing")
        
        dict.removeValue(forKey: "textColor")
        
        DZMUserDefaults.setObject(dict, key: DZMReadConfigureKey)
    }
    
    /// 清理(暂无需求使用)
    private func clear() {
        
        instance = nil
        
        DZMUserDefaults.removeObjectForKey(DZMReadConfigureKey)
    }

    func getparagraphStyle() -> NSMutableParagraphStyle {
       let paragraphStyle = NSMutableParagraphStyle()

        // 行间距
        var lineSpacing:CGFloat = 20
          // 段间距
        var paragraphSpacing:CGFloat = 30
         // 当前行间距(lineSpacing)的倍数(可根据字体大小变化修改倍数)
        var lineHeightMultiple:CGFloat = 1
        var headIndent:CGFloat = 20
        // 首行间距
        var firstLineHeadIndent =  CGFloat(28 + (fontSize * 2))

        switch paragraphStyleIndex {
        case 0:
            lineSpacing = 20
            paragraphSpacing = 30
            lineHeightMultiple = 1
            headIndent = 20
            firstLineHeadIndent = CGFloat(28 + (fontSize * 2))
            break
        case 1:
            // 行间距
            lineSpacing = 10
            paragraphSpacing = 20
            lineHeightMultiple = 1
            headIndent = 20
            firstLineHeadIndent = CGFloat(28 + (fontSize * 2))
            break
        case 2:
            lineSpacing = 5
            paragraphSpacing = 5
            lineHeightMultiple = 1
            headIndent = 20
            firstLineHeadIndent = CGFloat(28 + (fontSize * 2))
            break
        default:
            break
        }

        // 行间距
        paragraphStyle.lineSpacing = CGFloat(lineSpacing)

        // 段间距
        paragraphStyle.paragraphSpacing = paragraphSpacing

        // 当前行间距(lineSpacing)的倍数(可根据字体大小变化修改倍数)
        paragraphStyle.lineHeightMultiple = lineHeightMultiple

        paragraphStyle.headIndent = CGFloat(headIndent)

        paragraphStyle.firstLineHeadIndent = firstLineHeadIndent
        // 对其
        paragraphStyle.alignment = NSTextAlignment.justified

        paragraphStyle.lineBreakMode = .byWordWrapping

        return paragraphStyle
    }
    
    /// 获得文字属性字典
    func readAttribute() ->[String:NSObject] {

        let paragraphStyle = getparagraphStyle()
        // 返回
        return [NSForegroundColorAttributeName:DZMReadTextColors[DZMReadConfigure.shared().colorIndex],NSFontAttributeName:readFont(),NSParagraphStyleAttributeName:paragraphStyle]
    }
    
    
    /// 获取字体
    ///
    /// - Returns: 返回字体
    func readFont() ->UIFont {
        
        if fontType == DZMRMFontType.one.rawValue { // 黑体

            return UIFont(name: "DFWaWaSC-W5", size: CGFloat(fontSize))!

        }else if fontType == DZMRMFontType.two.rawValue { // 楷体

            return UIFont(name: "HanziPenSC-W3", size: CGFloat(fontSize))!

        }else if fontType == DZMRMFontType.three.rawValue { // 宋体

            return UIFont(name: "STKaitiTC-Regular", size: CGFloat(fontSize)) ?? UIFont(name: "DFWaWaSC-W5", size: CGFloat(fontSize))!

        }else{ // 系统

            return UIFont.systemFont(ofSize: CGFloat(fontSize))
        }

//         return UIFont(name: "DFWaWaSC-W5", size: CGFloat(fontSize)) ?? UIFont.systemFont(ofSize: CGFloat(fontSize))
    }
    

    /// 获得颜色
    func readColor() ->UIColor {
        
        if colorIndex == DZMReadBGColors.index(of: DZMReadBGColor_4) { // 牛皮黄
            
            return UIColor(patternImage:UIImage(named: "read_bg_0")!)
            
        }else{
            
            return DZMReadBGColors[colorIndex]
        }
    }
    
    
    
    // MARK: -- 构造初始化
    
    /// 创建获取内存中的用户信息
    class func readInfo() ->DZMReadConfigure {
        
        let info = DZMUserDefaults.objectForKey(DZMReadConfigureKey)
        
        return DZMReadConfigure(dict:info)
    }
    
    /// 初始化
    private init(dict:Any?) {
        
        super.init()
        
        setData(dict: dict)
    }
    
    /// 更新设置数据
    private func setData(dict:Any?) {
        
        if dict != nil {
            
            setValuesForKeys(dict as! [String : AnyObject])
        }
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {}
}
